function fsb_img_write(idat,fileName,header_template,scaninfo,gopt,typ_e)

% FSB :  write files in analyze format to disk
%
% EXAMPLE:
% fsb_img_write(idat,fileName,header_template,typ_e,gopt)
%
% INPUT:
% fileName: Name of file to write
% idat :     4-D image dataset or string
% header_template: template header file name or struct
% typ_e : Datatype of file to write
% gopt: Graphics output 1/0
%
% OUTPUT:
% files in analyze format
% 
% CALLED BY: 
% FSB.m
% fsb_export.m
% fsb_prepare_SPM.m
% fsb_realign_SPM.m
% fsb_SPM5.m
% fsb_SPM.m
% 
% NOTES:
% uses avw_img_write and spm_hwrite to write files.
% Using avw_img_write alone will result in faulty headers
%
% Steffen Stoewer 2007 stoewer@tuebingen.mpg.de
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
% 
% $Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~

h = waitbar(0,'Writing images...');

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Load a template header to fill in changed fields:
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

load avw.mat

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Get some information from data:
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[a b c d] = size(idat);
DIM =[a b c];% use dimensions of idat in spm header

gopt = 0; % Set this to save memory

try
    ORIGIN = scaninfo.origin(1:3);
catch
    ORIGIN = round(DIM/2); % set origin to middle of volume
    ORIGIN(3) = 0;
end

ORIGIN = int16(ORIGIN);

data_range = [min(idat(:)) max(idat(:))];

if isreal(idat)

    if max(idat(:))>intmax('int16') | isfloat(idat);
        
        TYPE = 64;
        avw.hdr.dime.bitpix =64;
    else
        TYPE =4;
        r = 1;
        idat = idat./r;
        idat = int16(idat);
        avw.hdr.dime.bitpix =16;
    end
else
            TYPE = 64;
        avw.hdr.dime.bitpix =64;
end
    
if isstruct(header_template)
    avw.hdr.dime.pixdim = header_template.dime.pixdim;

else
    avw.hdr.dime.pixdim = [0 1 1 1 0 0 0 0];

end

if exist('gopt','var')
    if gopt>0
    figure(1001); hist(double(idat(:)));
    end
end


%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Complete SPM header information here:
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OFFSET =0;
DESCRIP =fileName;
SCALE = 1;
%SCALE = 0.0039;
VOX = avw.hdr.dime.pixdim(2:4);

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Now write all possible information into avw header:
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

avw.hdr.dime.dim = [3 DIM 1 0 0 0]; % adjust avw.hdr
avw.hdr.dime.datatype = TYPE;
avw.hdr.dime.glmax = data_range(2); % int16
avw.hdr.dime.glmin = data_range(1); % int16
avw.hdr.dime.calmax = 1;
avw.hdr.dime.roi_scale = SCALE;
avw.hdr.hist.orient = 0;
avw.hdr.dime.pixdim(5) =1;
avw.hdr.dime.pixdim(8) =0; % put this in for compatibility issues
avw.hdr.hk.db_name = fileName;
avw.hdr.hist.generated = 'today';

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Display information
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

disp (['Data range from ' num2str(data_range(1)) ' to ' num2str(data_range(2))]);

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Write images
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if d<=1;

    waitbar(50/100)  ;
    avw.img = idat;
    avw_img_write(avw,fileName,0,'ieee-le');
    spm_hwrite(fileName,DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP);

else

    for x = 1:d;
        waitbar(x/d)  ;
        avw.img = idat(:,:,:,x);
        filename2 = sprintf('%05.0f', x);
        avw_img_write(avw,strcat(fileName,filename2),0,'ieee-le');
        spm_hwrite(strcat(fileName,filename2),DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP);
    end


end
close(h);
end
